package com.maimang.platform.dsp.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maimang.platform.dsp.constant.AdApi;
import com.maimang.platform.dsp.entity.AdvertiserIds;
import com.maimang.platform.dsp.entity.Budget;
import com.maimang.platform.dsp.mapper.BudgetMapper;
import com.maimang.platform.dsp.service.AccessTokenService;
import com.maimang.platform.dsp.service.AdvertiserIdsService;
import com.maimang.platform.dsp.service.BudgetService;
import com.maimang.platform.dsp.utils.ApiService;
import com.maimang.platform.lang.ConvertUtils;
import com.maimang.platform.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname BudgetServiceImpl
 * @description <br>广告日预算服务层实现类</br>
 * @date 2019-08-13 15:35:11
 * @email jintaozhao@qq.com
 */
@Service
public class BudgetServiceImpl implements BudgetService {

	@Autowired
	private BudgetMapper budgetMapper;

	@Autowired
	private ApiService apiService;

	@Autowired
	private AdvertiserIdsService advertiserIdsService;

	@Autowired
	private AccessTokenService accessTokenService;

	/**
	 * 查询广告日预算信息
	 *
	 * @param id 广告日预算ID
	 * @return 广告日预算信息
	 */
	@Override
	public Budget selectBudgetById(Long id) {
		return budgetMapper.selectBudgetById(id);
	}

	/**
	 * 同步广告主账户预算
	 *
	 * @param advertiserId 代理商广告主id
	 */
	@Override
	public void syncBudget(String advertiserId) {

		List<AdvertiserIds> idsList = advertiserIdsService.selectAdvertiserIds(Long.valueOf(advertiserId));

		double total = idsList.size();
		int pageSize = 2000;
		double totalPage = Math.ceil(total / pageSize);
		for (int i = 0; i < totalPage; i++) {
			int beginIndex = i * pageSize;
			int endIndex = (int) Math.min((i + 1) * pageSize, total);

			Executors.newCachedThreadPool().execute(() -> {
				List<AdvertiserIds> advertiserIdsList = idsList.subList(beginIndex, endIndex);

				int step = 50;
				for (int j = 0; j < advertiserIdsList.size(); j += step) {
					int length = Math.min(step, advertiserIdsList.size() - j);
					long[] ids = new long[length];

					for (int m = 0; m < length; m++) {
						ids[m] = advertiserIdsList.get(j + m).getId();
					}
					getBudget(ids);
				}
			});
		}
	}

	@Override
	public void getBudget(long[] advertiser_ids) {
		Map param = new HashMap();
		if (advertiser_ids.length > 0) {
			param.put("advertiser_ids", advertiser_ids);
		}

		String accessToken = accessTokenService.accessToken();
		if (StringUtils.isEmpty(accessToken))
			return;

		JSONObject result = apiService.sendGet(AdApi.BUDGET_GET, accessToken, param);
		if (result != null) {
			if (result.getInteger("code") == 0) {
				JSONObject data = result.getJSONObject("data");
				if (data != null) {
					JSONArray list = data.getJSONArray("list");

					List<Budget> budgetList = list.toJavaList(Budget.class);

					if (budgetList.size() > 0) {
						// 保存到数据库中
						for (Budget budget : budgetList) {
							budgetMapper.deleteBudgetById(budget.getAdvertiserId());
							budgetMapper.insertBudget(budget);
						}
					}
				}
			}
		}
	}

	/**
	 * 查询广告日预算列表
	 *
	 * @param budget 广告日预算信息
	 * @return 广告日预算集合
	 */
	@Override
	public List<Budget> selectBudgetList(Budget budget) {
		return budgetMapper.selectBudgetList(budget);
	}

	/**
	 * 新增广告日预算
	 *
	 * @param budget 广告日预算信息
	 * @return 结果
	 */
	@Override
	public int insertBudget(Budget budget) {
		return budgetMapper.insertBudget(budget);
	}

	/**
	 * 修改广告日预算
	 *
	 * @param budget 广告日预算信息
	 * @return 结果
	 */
	@Override
	public int updateBudget(Budget budget) {
		return budgetMapper.updateBudget(budget);
	}

	/**
	 * 删除广告日预算对象
	 *
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteBudgetByIds(String ids) {
		return budgetMapper.deleteBudgetByIds(ConvertUtils.toStrArray(ids));
	}

}
